﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;

namespace ZhCun.Framework.DataAccess.DbHelper
{
    public abstract class DbHelperBase : IDbHelper
    {
        /// <summary>
        /// 直接获得连接字符串
        /// </summary>
        /// <param name="connStr"></param>
        public DbHelperBase(string connStr)
        {
            _ConnStr = connStr;
        }
        protected string _ConnStr;
        bool _IsTrans = false;
        /// <summary>
        /// 是否开启事务
        /// </summary>
        //public bool IsTrans
        //{
        //    get
        //    {
        //        return _IsTrans;
        //    }
        //}
        protected abstract DbConnection DBConnectionObj { get; }
        protected abstract DbCommand DbCommandObj { get; }
        protected abstract DbDataAdapter DbDataAdapterObj { get; }
        protected DbTransaction DbTransObj;

        public DbConnection CurrentConnection
        {
            get
            {
                return DBConnectionObj;
            }
        }

        /// <summary>
        /// 打开连接,如果已经打开则什么都不执行了
        /// </summary>
        void OpenConnection()
        {
            if (DBConnectionObj.State != ConnectionState.Open)
            {
                DBConnectionObj.ConnectionString = _ConnStr;
                DBConnectionObj.Open();
            }
        }
        /// <summary>
        /// 给当前DbCommand对象赋值,并且OpenConnection();
        /// </summary>
        void SetCommandAndOpenConnect(string sqlText, CommandType cmdType, params DbParameter[] param)
        {
            //按说赋值Connection,CommandType,是不用多次赋值的
            DbCommandObj.CommandType = cmdType;
            DbCommandObj.Connection = DBConnectionObj;
            DbCommandObj.Parameters.Clear();
            if (param != null)
            {
                DbCommandObj.Parameters.AddRange(param);
            }
            DbCommandObj.CommandText = sqlText;
            OpenConnection();
        }
        /// <summary>
        /// 开始执行事务
        /// </summary>
        public void TransStart()
        {
            OpenConnection();
            DbTransObj = DBConnectionObj.BeginTransaction();
            DbCommandObj.Transaction = DbTransObj;
            _IsTrans = true;
        }
        /// <summary>
        /// 事务提交
        /// </summary>
        public void TransCommit()
        {
            _IsTrans = false;
            DbTransObj.Commit();
            CloseConnect();
        }
        /// <summary>
        /// 事务回滚
        /// </summary>
        public void TransRollback()
        {
            _IsTrans = false;
            DbTransObj.Rollback();
            CloseConnect();
        }
        /// <summary>
        /// 关闭连接,如果没有开始事务或连接打开时才关闭
        /// </summary>
        void CloseConnect()
        {
            if (!_IsTrans)
            {
                if (DBConnectionObj.State == ConnectionState.Open)
                {
                    DBConnectionObj.Close();
                    DBConnectionObj.Dispose();
                }
            }
        }
        /// <summary>
        /// 执行一条指定命令类型(SQL语句或存储过程等)的SQL语句,返回所影响行数
        /// </summary>
        public int ExecNonQuery(string sqlText, CommandType cmdType, params DbParameter[] param)
        {
            try
            {
                SetCommandAndOpenConnect(sqlText, cmdType, param);
                return DbCommandObj.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                CloseConnect();
            }
        }
        /// <summary>
        /// 执行一条普通SQL语句的命令,返回所影响行数
        /// </summary>
        public int ExecNonQuery(string sqlText, params DbParameter[] param)
        {
            return ExecNonQuery(sqlText, CommandType.Text, param);
        }
        /// <summary>
        /// 获得DataReader对象
        /// </summary>
        public DbDataReader GetDataReader(string sqlText, CommandType cmdType, params DbParameter[] param)
        {
            try
            {
                SetCommandAndOpenConnect(sqlText, cmdType, param);
                CommandBehavior cmdBehavior = CommandBehavior.CloseConnection;
                if (_IsTrans)
                {
                    cmdBehavior = CommandBehavior.Default;
                }
                DbDataReader dbReader = DbCommandObj.ExecuteReader(cmdBehavior);
                return dbReader;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //DataReader用dbReader对象来关闭
                //CloseConnect();
            }
        }
        /// <summary>
        /// 执行sql语句返回DataReader对象
        /// </summary>
        public DbDataReader GetDataReader(string sqlText, params DbParameter[] param)
        {
            return GetDataReader(sqlText, CommandType.Text, param);
        }
        /// <summary>
        /// 执行SQL语句,返回首行首列
        /// </summary>
        public object GetScalar(string sqlText, params DbParameter[] param)
        {
            return GetScalar(sqlText, CommandType.Text, param);
        }
        /// <summary>
        /// 获得首行首列
        /// </summary>
        public object GetScalar(string sqlText, CommandType cmdType, params DbParameter[] param)
        {
            try
            {
                SetCommandAndOpenConnect(sqlText, cmdType, param);
                return DbCommandObj.ExecuteScalar();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                CloseConnect();
            }
        }
        /// <summary>
        /// 执行一条SQL语句返回DataSet对象
        /// </summary>        
        public DataSet GetDataSet(string sqlText, params DbParameter[] param)
        {
            return GetDataSet(sqlText, CommandType.Text, param);
        }
        /// <summary>
        /// 执行一条SQL语句返回DataSet对象
        /// </summary>
        public DataSet GetDataSet(string sqlText, CommandType cmdType, params DbParameter[] param)
        {
            try
            {
                SetCommandAndOpenConnect(sqlText, cmdType, param);
                DbDataAdapterObj.SelectCommand = DbCommandObj;
                DataSet ds = new DataSet();
                DbDataAdapterObj.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                CloseConnect();
            }
        }
        /// <summary>
        /// 执行一条SQL语句返回DataTable对象(调用GetDataSet)
        /// </summary>        
        public DataTable GetDataTable(string sqlText, params DbParameter[] param)
        {
            return GetDataTable(sqlText, CommandType.Text, param);
        }
        /// <summary>
        /// 执行一条SQL语句返回DataTable对象(调用GetDataSet)
        /// </summary>
        public DataTable GetDataTable(string sqlText, CommandType cmdType, params DbParameter[] param)
        {
            return (GetDataSet(sqlText, cmdType, param)).Tables[0];
        }
    }
}